package com.leo.erp.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leo.erp.dao.MaterialStockMapper;
import com.leo.erp.dto.MaterialStockExecution;
import com.leo.erp.entity.MaterialStock;
import com.leo.erp.enums.MaterialStockStateEnum;
import com.leo.erp.service.LogService;
import com.leo.erp.service.MaterialStockService;
import com.leo.erp.util.GlobalRuntimeException;

@Service
public class MaterialStockServiceImpl implements MaterialStockService{
	@Autowired
	private MaterialStockMapper materialStockMapper;
	@Autowired
	private LogService logService;

	@Override
	public MaterialStockExecution getInitStockByMid(int depotId, int materialId) {
		double kucun = 0;
		MaterialStockExecution me = new MaterialStockExecution();
		QueryWrapper<MaterialStock> queryWrapper = new QueryWrapper<>();
		if(depotId > 0) {
			queryWrapper.eq("materialId", materialId);
			queryWrapper.eq("depotId", depotId);
			queryWrapper.ne("deleteFlag", "1");
		}else {
			queryWrapper.eq("materialId", materialId);
			queryWrapper.ne("deleteFlag", "1");
		}
		try {
			List<MaterialStock> list = materialStockMapper.selectList(queryWrapper);
			if(list.size() > 0) {
				for(MaterialStock ms : list) {
					if(ms != null) {
						kucun = kucun + ms.getNumber();
					}
				}
				me.setState(MaterialStockStateEnum.SUCCESS.getState());
				me.setKucun(kucun);
			}else {
				me.setState(MaterialStockStateEnum.OFFLINE.getState());
				me.setKucun(kucun);
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return me;
	}
	
	
}
